import TapUI from 'tap-ui'
import { MessageBox } from 'element-ui'

// 修改自定义列计算方式
TapUI.ListToolBar.methods.doUpdateTableColumn = function() {
  let isSelectColumn = false
  this.tableColumns.forEach((item) => {
    if (item.visible === true) {
      isSelectColumn = true
    }
  })
  if (isSelectColumn === false) {
    MessageBox.alert('至少选择一列', '错误', {
      dangerouslyUseHTMLString: true,
      type: 'error'
    })
    return
  }
  let lastColumnConfig
  this.tableColumns.forEach((item, columnIndex) => {
    var vm = this.getColumn(this.refMainTable, item) // this.refMainTable.$children.find(e => e.prop === item.code)
    // 列宽度设置
    if (vm !== undefined) {
      if (parseInt(item.width) > 0) {
        vm.columnConfig.width = parseInt(item.width)
      }
    }
    // 显示隐藏
    if (vm !== undefined && item.visible !== this.oldTableColumns[columnIndex].visible) {
      if (item.visible) {
        // 找出合适的插入点
        this.showColumn(vm, columnIndex + 1)
      } else {
        this.hideColumn(vm, vm.prop)
      }
    }
    // 最后一列可见列
    if (vm !== undefined && item.visible) {
      lastColumnConfig = vm.columnConfig
    }
  })
  // 最后一列可见列自适应大小， fix：将最小宽度改为width
  if (typeof lastColumnConfig !== 'undefined') {
    lastColumnConfig.minWidth = lastColumnConfig.width || 100
    lastColumnConfig.width = null
  }

  this.fixTableColumnPosition()
  this.oldTableColumns = JSON.parse(JSON.stringify(this.tableColumns))
  this.selectVisible = false
}

export default TapUI
